package com.example.exception;

import com.example.common.Result;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.ResponseBody;


// 全局异常处理器

@ControllerAdvice("com.example.antpluspermission.controller")
public class GlobalExceptionHandle {
    private static final Logger log = LoggerFactory.getLogger(GlobalExceptionHandle.class);

    @ExceptionHandler(Exception.class) // 拦截所有异常
    @ResponseBody // 返回json数据
    public Result error(Exception e) {
        log.error("系统异常",e);
        return Result.error(e.getMessage());
    }

    @ExceptionHandler(CustomException.class) // 拦截所有异常
    @ResponseBody // 返回json数据
    public Result customError(CustomException e) {
        log.error("自定义异常",e);
        return Result.error(e.getStatus(),e.getMessage());
    }
}

